package cn.kgc.springcloud.service.impl;

import cn.kgc.springcloud.clients.AccountClient;
import cn.kgc.springcloud.clients.StorageClient;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springcloud.entity.Order;
import cn.kgc.springcloud.service.OrderService;
import cn.kgc.springcloud.mapper.OrderMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author mengshun
* @description 针对表【order_tbl】的数据库操作Service实现
* @createDate 2025-01-16 08:59:36
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService{

    @Resource
    private StorageClient storageClient;

    @Resource
    private AccountClient accountClient;


    @GlobalTransactional   //  分布式全局事务管理   TM   -> TC
    @Override
    public Boolean createOrder(String userId, String commodityCode, Integer orderCount,Integer money) {

        Order order = new Order();
        order.setUserId(userId);
        order.setCommodityCode(commodityCode);
        order.setCount(orderCount);
        order.setMoney(money);

        // 创建订单
        baseMapper.insert(order);
        // 减少库存
        storageClient.deduct(commodityCode,orderCount);   //RM -> TC
        // 扣除余额
        accountClient.debit(userId,money);   // RM  -> TC


        return true;
    }
}




